using System;
using System.Data;
using System.Globalization;
using System.Web;
using Sedna.Core.Common.Objects.Navigation;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using Sedna.Core.Globalization;

namespace Sedna.Core.Controls.Actions
{
	/// <summary>
	/// Summary description for LoadMonthListAction.
	/// </summary>
	public class LoadMonthListAction : AbstractAction
	{
		private IMessageManager messageManager;
		private string dataSourcePath = null;
		private string selectedIndexPath = null;


		#region Public properties

		public IMessageManager MessageManager
		{
			set { messageManager = value; }
		}

		public string DataSourcePath
		{
			set { dataSourcePath = value; }
		}

		public string SelectedIndexPath
		{
			set { selectedIndexPath = value; }
		}

		#endregion


		public override IActionResult Execute(IExecutionContext executionContext)
		{
			DataTable table = new DataTable();
			table.Columns.Add("Value", typeof(string));
			table.Columns.Add("Text", typeof(string));

			int selectedIndex = DateTime.Now.Month - 1;
			if (selectedIndexPath != null)
			{
				try
				{
					int parsedValue = (int) scopePathParser.Parse(selectedIndexPath).Value;
					selectedIndex = parsedValue == -1 ? selectedIndex : parsedValue;
				}
				catch
				{
					selectedIndex = DateTime.Now.Month - 1;
				}
			}
			
			int index = 1;
			string[] months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
			foreach (string month in months)
			{
				if (month == null || String.Empty.Equals(month))
				{
					break;
				}
				DataRow row = table.NewRow();
				row["Value"] = (index++).ToString();
				row["Text"] = month;
				table.Rows.Add(row);
			}

			if (dataSourcePath != null)
			{
				executionContext.SetPropertyValue(dataSourcePath, table.DefaultView);
			}
			if (selectedIndexPath != null)
			{
				executionContext.SetPropertyValue(selectedIndexPath, selectedIndex);
			}

			return CreateResult(successStatus);
		}



	}
}
